{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-2-82ffd8de7016>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-2-82ffd8de7016>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    <!-- 分析地址数据的格式：\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# 分析地址数据的格式：\n",
    "# col   含义\n",
    "# 0     ID\n",
    "# 1     运单ID\n",
    "# 2     类型\n",
    "# 3     省\n",
    "# 4     市\n",
    "# 5     区\n",
    "# 6     gpsX\n",
    "# 7     gpsY\n",
    "# 8     主节点\n",
    "# 9     顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█████████████████████████████████████████████| 83/83 [07:02<00:00,  5.09s/it]\n"
     ]
    }
   ],
   "source": [
    "address_A_path = r'data/address_A_easy.txt'\n",
    "waybill_A_path = r'data/waybill_A.txt'\n",
    "\n",
    "address_A = pd.read_csv(address_A_path,header=None)\n",
    "waybill_A = pd.read_csv(waybill_A_path,header=None)\n",
    "# print(address_A)\n",
    "# print(waybill_A)\n",
    "\n",
    "\n",
    "loadCity_A_path = r'data/loadCity_A.txt'\n",
    "UnloadCity_A_path = r'data/UnloadCity_A.txt'\n",
    "\n",
    "loadCity_A = pd.read_csv(loadCity_A_path,header=None)\n",
    "UnloadCity_A = pd.read_csv(UnloadCity_A_path,header=None)\n",
    "\n",
    "caseNumber1 = loadCity_A.index.stop\n",
    "caseNumber2 = UnloadCity_A.index.stop\n",
    "\n",
    "result_out=[]\n",
    "\n",
    "\n",
    "for p in range(0,caseNumber1):\n",
    "    for q in tqdm(range(0,caseNumber2)):\n",
    "#         print(loadCity_A.loc[p].values[0],loadCity_A.loc[p].values[1],\n",
    "#               UnloadCity_A.loc[q].values[0],UnloadCity_A.loc[q].values[1])\n",
    "        \n",
    "#         Pos_A_Pro=r\"陕西\"\n",
    "#         Pos_A_City=r\"咸阳\"\n",
    "#         Pos_B_Pro=r\"陕西\"\n",
    "#         Pos_B_City=r\"西安\"\n",
    "        Pos_A_Pro =loadCity_A.loc[p].values[0]\n",
    "        Pos_A_City=loadCity_A.loc[p].values[1]\n",
    "        Pos_B_Pro= UnloadCity_A.loc[q].values[0]\n",
    "        Pos_B_City=UnloadCity_A.loc[q].values[1]\n",
    "\n",
    "        currentId=''\n",
    "        caseNumber = address_A.index.stop\n",
    "        i = 0\n",
    "        count = 1\n",
    "        resultIds=[]\n",
    "        while i<caseNumber:\n",
    "\n",
    "            address= address_A.loc[i].values\n",
    "            addressId=address[1]\n",
    "            addressPro=address[3]\n",
    "            addressCity=address[4]\n",
    "            #print(addressId,addressPro,addressCity)\n",
    "\n",
    "            i=i+1\n",
    "            if addressId==currentId: #如果不是新的ID\n",
    "                continue\n",
    "            else: #如果是新的运单ID\n",
    "                currentId=addressId\n",
    "                if addressPro==Pos_A_Pro and addressCity==Pos_A_City:\n",
    "                    #如果是目标提货点Pos_A,直接搜索下一条数据\n",
    "                    address= address_A.loc[i].values\n",
    "                    addressId=address[1]\n",
    "                    addressPro=address[3]\n",
    "                    addressCity=address[4]\n",
    "                    if addressPro==Pos_B_Pro and addressCity==Pos_B_City:\n",
    "                        #如果是目标卸货点Pos_B\n",
    "                        #在运单数据中，记录该订单\n",
    "\n",
    "                        resultIds.append(addressId)\n",
    "                        #print(count,addressId)\n",
    "                        #count=count+1\n",
    "\n",
    "                else:#如果不是目标提货点Pos_A\n",
    "                    continue\n",
    "\n",
    "            i=i+1\n",
    "\n",
    "\n",
    "        #进行核心计算\n",
    "        # address= address_A.loc[i].values\n",
    "        caseNumber = waybill_A.index.stop\n",
    "        i = 0\n",
    "        vols=[0]*19\n",
    "        tons=[0]*19\n",
    "\n",
    "        while i<caseNumber:\n",
    "            waybill=waybill_A.loc[i].values\n",
    "\n",
    "            times = waybill[2].split('-') #拿到运单开始时间分段\n",
    "            year=times[0]\n",
    "            month=times[1]\n",
    "            currentId=waybill[0]\n",
    "            vol=waybill[4]\n",
    "            ton=waybill[5]\n",
    "        #     print(currentId,year,month,vol,ton)\n",
    "\n",
    "            if currentId in resultIds:\n",
    "                Id=(int(year)-2018)*12+int(month)-1\n",
    "                vols[Id]=vols[Id]+vol\n",
    "                tons[Id]=tons[Id]+ton\n",
    "\n",
    "            i=i+1\n",
    "\n",
    "        # print(len(vols),vols)\n",
    "        # print(len(tons),tons)\n",
    "        \n",
    "        result_out.append([Pos_A_Pro,\n",
    "                          Pos_A_City,\n",
    "                          Pos_B_Pro,\n",
    "                          Pos_B_City,\n",
    "                          vols,\n",
    "                          tons]\n",
    "                          )\n",
    "        \n",
    "   \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     0   1   2     3                                                  4  \\\n",
      "0   陕西  咸阳  陕西    西安  [2130.0, 360.0, 5274.0, 4514.15, 5635.0, 5124....   \n",
      "1   陕西  咸阳  宁夏    银川  [25.0, 0, 275.0, 398.0, 430.0, 490.0, 427.0, 3...   \n",
      "2   陕西  咸阳  陕西    咸阳  [65.0, 80.0, 549.0, 273.0, 363.0, 275.0, 440.0...   \n",
      "3   陕西  咸阳  重庆    重庆  [350.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 35.0, 0,...   \n",
      "4   陕西  咸阳  陕西    汉中  [415.0, 60.0, 241.0, 292.0, 295.0, 445.0, 220....   \n",
      "..  ..  ..  ..   ...                                                ...   \n",
      "78  陕西  咸阳  湖北    荆州  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...   \n",
      "79  陕西  咸阳  新疆  石河子市  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...   \n",
      "80  陕西  咸阳  河南    济源  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...   \n",
      "81  陕西  咸阳  新疆    喀什  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...   \n",
      "82  陕西  咸阳  西藏    山南  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...   \n",
      "\n",
      "                                                    5  \n",
      "0   [868.1799999999997, 404.20000000000005, 3622.4...  \n",
      "1   [24.75, 0, 306.05999999999995, 466.33, 400.969...  \n",
      "2   [11.6, 67.69999999999999, 235.00000000000003, ...  \n",
      "3   [322.2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 57.5, 0,...  \n",
      "4   [239.52, 144.60000000000002, 240.09, 366.42, 2...  \n",
      "..                                                ...  \n",
      "78  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...  \n",
      "79  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...  \n",
      "80  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...  \n",
      "81  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...  \n",
      "82  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...  \n",
      "\n",
      "[83 rows x 6 columns]\n"
     ]
    }
   ],
   "source": [
    "df=pd.DataFrame(result_out)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "outputpath=r'data/result.csv'\n",
    "df.to_csv(outputpath,index=False,header=False,encoding='utf_8_sig')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
